package com.xxxx.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置客户端的编码格式
        req.setCharacterEncoding("UTF-8");
        //接收客户端传递的参数
        String uname=req.getParameter("uname");
        String upwd=req.getParameter("upwd");
        //判断参数是否为空
        if(uname==null||"".equals(uname.trim()))
        {
            req.setAttribute("msg","姓名不能为空");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
        if(upwd==null||"".equals(upwd.trim()))
        {
            req.setAttribute("msg","密码不能为空");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
        if(!"admin".equals(uname)||!"admin".equals(upwd))
        {
            req.setAttribute("msg","登录失败");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
        req.getSession().setAttribute("uname",uname);
        resp.sendRedirect("index.jsp");
    }
}
